package dao;

import entity.Appointment;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

public class AppointmentDAO extends DAO<Appointment> {

    public AppointmentDAO(EntityManagerFactory emf) {
        super(emf);
    }

    @Override
    protected Root from(CriteriaQuery cq) {
        return cq.from(Appointment.class);
    }

    @Override
    protected boolean exists(Appointment object) {
        EntityManager entityManager = getEntityManager();
        Query query = entityManager.createQuery("SELECT a FROM Appointment a WHERE a.id = :id");
        query.setParameter("id", object.getId());
        
        return !query.getResultList().isEmpty();
    }

}
